package cn.fl.ref.fncprjdrawmon.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjdrawmon.service.IFncPrjDrawMonService;
import cn.fl.ref.fncprjdrawmon.vo.FncPrjDrawMonVO;
import cn.fl.system.service.ISysBkAccInfoService;
import cn.fl.system.vo.SysBkAccInfoVO;

import com.alibaba.fastjson.JSONObject;

import fl.ref.fncprjdrawmon.condition.FncPrjDrawMonQueryCondition;


/**
 * @Description: 再融资项目到账明细表
 */
@Controller
@RequestMapping("/rfbuss/fncprjdrawmon")
public class FncPrjDrawMonController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FncPrjDrawMonController.class);

	@Autowired
	IFncPrjDrawMonService fncPrjDrawMonService;
	@Autowired
	private ISysBkAccInfoService sysBkAccInfoService;//我司账户
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("加载crbaseinfo列表页面.");
	//	String fncInsId  = request.getParameter("fncInsId");
	//	String fncInsNm  = request.getParameter("fncInsNm");
	//	String fncInsShort  = request.getParameter("fncInsShort");
		ModelAndView view = new ModelAndView("/rfbuss/fncprjdrawmon/fncprjdrawmonList");
	//	view.addObject("fncInsId", fncInsId);
	//	view.addObject("fncInsNm", fncInsNm);
	//	view.addObject("fncInsShort", fncInsShort);
		return view;
	}
	
	/**
	 * 初始化列表
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
	
	
		String id = request.getParameter("id");
	
		
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		FncPrjDrawMonQueryCondition qc=new FncPrjDrawMonQueryCondition();
		
	
		if (StringUtils.isNotEmpty(id)) {
			qc.setPrjId(Long.parseLong(id));
		}
		
		
		page.setCondition(qc);
		
		//Pager pager = 
		List<FncPrjDrawMonVO> list = (List<FncPrjDrawMonVO>) this.fncPrjDrawMonService.searchList(qc);//pager.getResultList();
		setAccInfo(list);//设置账户信息：我司账户
		ListPageVO<FncPrjDrawMonVO> listPage = new ListPageVO<FncPrjDrawMonVO>(list, 1);

		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	//设置账户信息：我司账户
		private void setAccInfo(List<FncPrjDrawMonVO> fncPrjDrawMonVOList) {
			if(fncPrjDrawMonVOList != null && fncPrjDrawMonVOList.size() >0) {
				for (FncPrjDrawMonVO fncPrjDrawMonVO : fncPrjDrawMonVOList) {
					if(fncPrjDrawMonVO.getAccId() != null) {
						SysBkAccInfoVO sysBkAccInfoVO = sysBkAccInfoService.selectById(fncPrjDrawMonVO.getAccId());
						if(sysBkAccInfoVO != null) {
							fncPrjDrawMonVO.setDmAccNm(sysBkAccInfoVO.getAccNm());//我司账户
						}
					}
				}
			}
		}
		
	/**
	 * 到授信新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		String id  = request.getParameter("id");
		
		ModelAndView view = new ModelAndView("/rfbuss/fncprjdrawmon/fncPrjDrawMonAdd");
		//view.addObject("id", id);
		return view;
	}
	
	
	
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws Exception {
String dmTm = request.getParameter("dmTm");

String dmAm = request.getParameter("dmAm");

String dueBank = request.getParameter("dueBank");

String dmSt = request.getParameter("dmSt");

String dmAcc = request.getParameter("dmAcc");

String prjId = request.getParameter("prjId");

String accId = request.getParameter("accId");

		
		
		FncPrjDrawMonVO  entity=new FncPrjDrawMonVO();
		if (StringUtils.isNotEmpty(dmTm)) {
			
		}
		if (StringUtils.isNotEmpty(dmAm)) {
			
		}
		if (StringUtils.isNotEmpty(dueBank)) {
          entity.setDueBank(dueBank);
			
		}
		if (StringUtils.isNotEmpty(dmSt)) {
			
		}
		if (StringUtils.isNotEmpty(dmAcc)) {
          entity.setDmAcc(dmAcc);
			
		}
		if (StringUtils.isNotEmpty(prjId)) {
			
		}
		if (StringUtils.isNotEmpty(accId)) {
			
		}

		
		//entity.setIsvalid(BaseConstant.IS_YESNO_YES); // 有效
		//entity.setIsdel(BaseConstant.IS_YESNO_NO); // 未删除
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		
		try {
			Long mainId = this.fncPrjDrawMonService.add(entity);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("新增成功!");
			BaseLogUtils.info(logger, "add", "新增数据 ", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
			BaseLogUtils.error(logger, "add", "新增数据失败"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/remove.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public DataResultVO remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		try {
			this.fncPrjDrawMonService.removeById(Long.parseLong(id));
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("删除成功!");
			BaseLogUtils.info(logger, "remove", "删除数据,主键["+id+"].", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "删除数据失败,主键["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "remove", "删除数据失败,主键["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	
}

